package com.example.SBootDemo.Util.MINA.infoAction;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;

import com.example.SBootDemo.Util.MINA.infoModel.DeviceModel;


public class BizManager{	
	private static BizManager instance = new BizManager();
	
	private BlockingQueue<DeviceModel> msgQueue = new LinkedBlockingQueue<DeviceModel>();
	
	
	public static BizManager getInstance() {
		return instance;
	}
	
	private BizManager() {}
	
	public void open() {
		Thread t = new Thread(new BizAction());
		t.start();
	}
	
	public void putMessage(DeviceModel block) {
		try {
			msgQueue.put(block);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private class BizAction implements Runnable {
		private Executor executor = Executors.newCachedThreadPool();
		@Override
		public void run() {
			while (true) {
				try {				
					final DeviceModel block = msgQueue.take();
					if (null != block) {
						executor.execute(new Runnable(){
							
							public void run(){
								handle(block);
							}
						});
					}				
				} catch (InterruptedException e) {
					Logger.getLogger(BizManager.class).error("Exception", e);
				}
			}
		}
		
		private void handle(DeviceModel block) {
//			MessageAction action = null;
			
//			switch (block.getDeviceType()) {
//			case T808: action = T808MessageAction.getInstance();  break;
//			case CMS: action = CMSMessageAction.getInstance();    break;			
//			case N9M: action = N9MMessageAction.getInstance();    break;			
//			default:
//				break;
//			}
//			
//			if (null != action) {
//				action.doAction(block);
//			}
			
		}
	}
}
